package com.yuer.zk.zk;

import org.I0Itec.zkclient.DataUpdater;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;

import java.util.List;

public class ZkCliTest_1 {



    public static void main(String[] args) throws InterruptedException {
        ZkClient zkClient = new ZkClient("192.168.56.103:2182");
        System.out.println("创建成功啦");
        String parentParentPath = "/zkcli-11";
        if(!zkClient.exists(parentParentPath)){
            String result =  zkClient.create(parentParentPath,"123", CreateMode.PERSISTENT);
            System.out.println(result);
        }
//        zkClient.deleteRecursive(parentParentPath);
//        zkClient.createPersistent(parentParentPath+"/2344/1234",true);
//        if(!zkClient.exists(parentParentPath+"/2344/1234")){
//            System.out.println(parentParentPath+"/2344"+" 节点被创建");
//        }else {
//            System.out.println(parentParentPath+"/2344"+" as 节点被创建");
//        }
//
//        zkClient.subscribeDataChanges("/parentParentPath", new IZkDataListener() {
//            @Override
//            public void handleDataChange(String s, Object o) throws Exception {
//                System.out.println("s："+s+"  "+0);
//            }
//
//
//            @Override
//            public void handleDataDeleted(String s) throws Exception {
//                System.out.println("执行了删除的操作："+s);
//            }
//        });
//        zkClient.subscribeChildChanges(parentParentPath, new IZkChildListener() {
//            @Override
//            public void handleChildChange(String parentPath, List<String> list) throws Exception {
//                System.out.println(parentPath + " s child changed"+
//                        list+" + currentChilds");
//            }
//        });
//        zkClient.createPersistent(parentParentPath+"/2344/123433","12341341234");
//        Thread.sleep(1000);
//        zkClient.create(parentParentPath+"/2344/1234/5","12341341234",CreateMode.PERSISTENT);
//
//        Thread.sleep(Integer.MAX_VALUE);
       String path  = "/zok-test—persistent";
       List<String> list =  zkClient.getChildren(path);
       System.out.println(list);

        zkClient.subscribeChildChanges(path + "/get1", new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> list) throws Exception {
                System.out.println(parentPath+"      " + list);
            }
        });
//        zkClient.subscribeDataChanges(path + "/get1", new IZkDataListener() {
//            @Override
//            public void handleDataChange(String parentPath, Object o) throws Exception {
//                System.out.println(parentPath+"   ----   " + o);
//            }
//
//            @Override
//            public void handleDataDeleted(String s) throws Exception {
//                System.out.println("执行了删除的操作："+s);
//            }
//        });
        zkClient.createPersistent(path + "/get1",true);
        Thread.sleep(1000);
        zkClient.createPersistent(path + "/get1/123",true);
        Thread.sleep(1000);
        zkClient.createPersistent(path + "/get1/1234",true);
        Thread.sleep(1000);
        zkClient.createPersistent(path + "/get1/1235",true);
        Thread.sleep(1000);
        zkClient.deleteRecursive(path + "/get1");
        Thread.sleep(1000);
        zkClient.subscribeDataChanges(path + "/get3", new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println("节点的路径"+s+"----------  内容： " +o);
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                System.out.println("该节点给删除啦： " +s);
            }
        });
        zkClient.createEphemeral(path + "/get3");
        Thread.sleep(1000);
        //写入节点
        zkClient.writeData(path + "/get3","这是我的先入得节点");
        Thread.sleep(1000);
        zkClient.updateDataSerialized(path + "/get3", new DataUpdater<String>() {
            @Override
            public String update(String o) {

                return "這是我更新之后的节点";
            }
        });
        zkClient.deleteRecursive(path + "/get3");
        Thread.sleep(1000);
     }

}
